package com.tobeface.sns.interfaces;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.web.util.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kissme.core.web.Webs;
import com.kissme.lang.Files;
import com.tobeface.sns.application.blog.BlogCommentService;
import com.tobeface.sns.application.blog.BlogService;
import com.tobeface.sns.domain.User;
import com.tobeface.sns.domain.blog.BlogComment;
import com.tobeface.sns.interfaces.util.JsonMessage;
import com.tobeface.sns.interfaces.util.UserUtil;

/**
 * @author Linxiaosheng
 */
@Controller
public class BlogCommentController {

	@Autowired
	private BlogCommentService blogCommentService;
	@Autowired
	private BlogService blogService;
	
	@RequestMapping(value = "/blog-comment/{id}/create", method = RequestMethod.POST) 
	@ResponseBody
	public JsonMessage create(@RequestHeader(value = "X-Requested-With", required = false) String requestedWith, 
						 @PathVariable("id")String id,
						 HttpServletRequest request) {
		
		if(!Webs.isAjax(requestedWith)) {
			return null;
		}
		
		User user = UserUtil.getCurrentLoginUser();
		
		String comment = WebUtils.getCleanParam(request, "comment");
		
		BlogComment blogComment = new BlogComment().setBlog(blogService.findOne(id)).setComment(comment).setUser(user);
		
		blogCommentService.create(blogComment);
		
		return JsonMessage.one().ok().message("success");
	}

	public String delete(String id) {
		blogCommentService.delete(id);
		return Files.asUnix("/blog/list"); 
	}

	public String delete(HttpServletRequest request) {
		
		return null;
	}

}
